package com.gwnet.shop.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.gwnet.common.core.utils.DateUtils;
import com.gwnet.shop.domain.ShopGoods;
import com.gwnet.shop.domain.SpecGroup;
import com.gwnet.shop.domain.vo.SpecGroupVo;
import com.gwnet.shop.mapper.ShopGoodsMapper;
import com.gwnet.shop.mapper.SpecGroupMapper;
import org.springframework.stereotype.Service;
import com.gwnet.shop.mapper.ShopCartMapper;
import com.gwnet.shop.domain.ShopCart;
import com.gwnet.shop.service.IShopCartService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 购物车Service业务层处理
 * 
 * @author zhangwanpeng
 * @date 2024-08-06
 */
@Service
public class ShopCartServiceImpl implements IShopCartService 
{
    @Resource
    private ShopCartMapper shopCartMapper;

    @Resource
    private SpecGroupMapper specGroupMapper;

    @Resource
    private ShopGoodsMapper shopGoodsMapper;

    /**
     * 查询购物车
     * 
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public SpecGroupVo selectShopCartById(Long id)
    {
        ShopCart shopCart = shopCartMapper.selectShopCartById(id);

        SpecGroup specGroup = specGroupMapper.selectSpecGroupById(shopCart.getSpecGroupId());
        ShopGoods shopGoods = shopGoodsMapper.selectShopGoodsById(specGroup.getGoodsId());

        specGroup.setShopGoods(shopGoods);
        SpecGroupVo specGroupVo = new SpecGroupVo();
        specGroupVo.setSpecGroup(specGroup);
        specGroupVo.setQuantity(shopCart.getQuantity());
        return specGroupVo;
    }

    /**
     * 查询购物车列表
     * @param cart 购物车
     * @return 购物车
     */
    @Override
    public List<SpecGroupVo> selectShopCartList(ShopCart cart)
    {
        List<ShopCart> shopCarts = shopCartMapper.selectShopCartList(cart);
        List<SpecGroupVo> specGroupVos = new ArrayList<>();
        for(ShopCart shopCart: shopCarts){
            SpecGroup specGroup = specGroupMapper.selectSpecGroupById(shopCart.getSpecGroupId());
            ShopGoods shopGoods = shopGoodsMapper.selectShopGoodsById(specGroup.getGoodsId());
            specGroup.setShopGoods(shopGoods);
            SpecGroupVo specGroupVo = new SpecGroupVo();
            specGroupVo.setSpecGroup(specGroup);
            specGroupVo.setQuantity(shopCart.getQuantity());
            specGroupVos.add(specGroupVo);
        }
        return specGroupVos;
    }

    /**
     * 新增购物车
     * 
     * @param shopCart 购物车
     * @return 结果
     */
    @Transactional
    @Override
    public int insertShopCart(ShopCart shopCart)
    {
        ShopCart cart = shopCartMapper.getShopCart(shopCart);
        if(cart == null){
            shopCart.setCreateTime(DateUtils.getNowDate());
            return shopCartMapper.insertShopCart(shopCart);
        }else{
            cart.setQuantity( cart.getQuantity() + shopCart.getQuantity());
            return shopCartMapper.updateShopCart(cart);
        }
    }

    /**
     * 修改购物车
     * 
     * @param shopCart 购物车
     * @return 结果
     */
    @Override
    public int updateShopCart(ShopCart shopCart)
    {
        return shopCartMapper.updateShopCart(shopCart);
    }

    /**
     * 批量删除购物车
     * 
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopCartByIds(Long[] ids)
    {
        return shopCartMapper.deleteShopCartByIds(ids);
    }

    /**
     * 删除购物车信息
     * 
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopCartById(Long id)
    {
        return shopCartMapper.deleteShopCartById(id);
    }
}
